#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    manager = new QNetworkAccessManager(this);
    connect(manager,QNetworkAccessManager::finished,this,Widget::requestReply);
    setCursor(QCursor(QPixmap(":/image/cat.ico")));
    ui->textEdit->setCursor(QCursor(QPixmap(":/image/cat.ico")));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::sentHttpsRequest()
{
    request_url = QString("https://m.douban.com/rexxar/api/v2/gallery/topic/%1/items?from_web=1&sort=hot&start=%2&count=20&status_full_text=1&guest_only=0&ck=").arg(ui->lineEdit->text(),QString::number(requested_time));
    QNetworkRequest request;
    request.setUrl(request_url);
    request.setRawHeader("Referer","https://www.douban.com/gallery/topic/3569588/?from=gallery_trend");
    request.setRawHeader("cookie",ui->lineEdit->text().toUtf8());
    manager->get(request);
}

void Widget::on_toolButton_clicked()
{
    requested_time = 0;
    ui->progressBar->setValue(0);
    if(ui->lineEdit->text() == "" || ui->label->text() == "" || ui->textEdit->toPlainText() == ""){
        QMessageBox::critical(this,"缺少必要字段","请将必要字段填写完整以进行数据爬取!");
        return ;
    }
    request_url = QString("https://m.douban.com/rexxar/api/v2/gallery/topic/%1/items?from_web=1&sort=hot&start=%2&count=20&status_full_text=1&guest_only=0&ck=").arg(ui->lineEdit->text(),QString::number(requested_time));
    QNetworkRequest request;
    request.setUrl(request_url);
    request.setRawHeader("Referer","https://www.douban.com/gallery/topic/3569588/?from=gallery_trend");
    request.setRawHeader("cookie",ui->lineEdit->text().toUtf8());
    manager->get(request);
}

void Widget::requestReply(QNetworkReply *reply)
{
    QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
    QJsonObject obj = doc.object();
    QJsonArray data_arr = obj.value("items").toArray();
    for(int i = 0;(i<data_arr.size())&&(requested_time < ui->spinBox->value());++i){
        QFile file(file_dir+"/评论.csv");
        file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
        QTextStream txtOutput(&file);
        qDebug()<<data_arr.at(i).toObject().value("target").toObject().value("status").toObject().value("text").toString();
        txtOutput<<QString::number(requested_time+1)<<','<<data_arr.at(i).toObject().value("target").toObject().value("status").toObject().value("text").toString().remove("\r\n")<<'\n';
        ++requested_time;
        ui->progressBar->setValue((requested_time/ui->spinBox->value())*100);
        ui->textEdit_2->append(QString("第 %1 个评论获取成功").arg(requested_time));
        file.close();
    }
    if(requested_time < ui->spinBox->value())
        sentHttpsRequest();
    else{
        ui->textEdit_2->append(QString("%1 个评论爬取成功!").arg(ui->spinBox->value()));
        ui->progressBar->setValue(100);
    }
}

void Widget::on_toolButton_2_clicked()
{
    file_dir = QFileDialog::getExistingDirectory(this,"");
    ui->label->setText(file_dir);
}
